﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Maticsoft.Common;
using System.Configuration;
using System.Net.NetworkInformation;
using System.Net;
using System.Data.OleDb;

namespace ZG_member
{
    public partial class email : Form
    {
        List<string> mail = new List<string>();
        int emailid ;
        public email(List<string> eamil,int id)
        {
            InitializeComponent();
            mail = eamil;
            emailid = id;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            top = this.Top;
            
        }
        BLL.EmailBLL bll = new BLL.EmailBLL();
       
        int top;
        public string TransferData(string excelFile, string sheetName)
        {

            //ZG_member.BLL.memberInfo adv = new ZG_member.BLL.memberInfo();
            //ZG_member.Model.memberInfo advMode = new ZG_member.Model.memberInfo();
            //DataTable advTB = adv.GetList("").Tables[0];

            DataSet ds = new DataSet();

            string excel = "";
            try
            {
                try
                {
                    //获取全部数据   
                    string strConn = "Provider=Microsoft.ACE.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended properties=\"Excel 12.0;Imex=1;HDR=Yes;\"";
                    OleDbConnection conn = new OleDbConnection(strConn);
                    conn.Open();
                    string strExcel = "";
                    OleDbDataAdapter myCommand = null;
                    strExcel = string.Format("select * from [{0}$]", sheetName);
                    myCommand = new OleDbDataAdapter(strExcel, strConn);
                    myCommand.Fill(ds, sheetName);
                    bool bl = true;
                    progressBar1.Maximum = ds.Tables[0].Rows.Count;
                    
                    //如果目标表不存在则创建   
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        excel += ds.Tables[0].Rows[i][0].ToString() + ",";
                    }
                    excel = excel.Substring(0, excel.Length - 1);
                 
                }
                catch (Exception ex)
                {
                    string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + excelFile + ";" + "Extended properties=\"Excel 12.0;Imex=1;HDR=Yes;\"";
                    OleDbConnection conn = new OleDbConnection(strConn);
                    conn.Open();
                    string strExcel = "";
                    OleDbDataAdapter myCommand = null;
                    strExcel = string.Format("select * from [{0}$]", sheetName);
                    myCommand = new OleDbDataAdapter(strExcel, strConn);
                    myCommand.Fill(ds, sheetName);
                    bool bl = true;
                    progressBar1.Maximum = ds.Tables[0].Rows.Count;
                    //如果目标表不存在则创建   
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        excel += ds.Tables[0].Rows[i][0].ToString() + ",";
                 
                    }
                    excel = excel.Substring(0, excel.Length - 1);
                 

                }
                System.Windows.Forms.MessageBox.Show("导入成功");
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("请确保Excel格式内容正确!");
                //return;
            }

            return excel;

        }






        private void email_Load(object sender, EventArgs e)
        {
            string s = Application.StartupPath + @"\Html\index.html";
            this.webBrowser1.Navigate(s);
            this.timer1.Start();
            Model.EmailModel emmod = bll.GetModel(1);
            this.txtFajianren.Text = emmod.EmailUser;
            this.txtFajianrenmima.Text = emmod.EmailPwd;
            if (emailid == 1)
            {
                this.Text = "群发邮件";
                
            }
            if (emailid == 0)
            {
                this.txtshoujianren.ReadOnly = true;
                for (int i = 0; i < mail.Count; i++)
                {
                    string[] go = mail[i].Split(',');

                    if (go[0] == null && go[1] == null)
                    {
                        break;
                    }
                    this.txtshoujianren.Text += go[1] + ",";
                }
            }
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://sdk2.entinfo.cn:8060/webservice.asmx");
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    resp.Close();
                }
                else
                {
                    this.button1.Enabled = false;
                    this.button2.Enabled = false;
                    System.Windows.Forms.MessageBox.Show("由于您的电脑未联网！邮件功能无法使用！");
                }
            }
            catch (WebException webex)
            {

                this.button1.Enabled = false;
                this.button2.Enabled = false;
                System.Windows.Forms.MessageBox.Show("由于您的电脑未联网！邮件功能无法使用！");

            }
            //webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.txtFajianren.Text.Trim() == "")
            {
                System.Windows.Forms.MessageBox.Show("请输入发件人邮箱！");
                return;
            }
            if (this.txtFajianrenmima.Text.Trim() == "")
            {
                System.Windows.Forms.MessageBox.Show("请输入发件人密码！");
                return;
            }
            if (this.txtshoujianren.Text.Trim() == "")
            {
                System.Windows.Forms.MessageBox.Show("请输入收件人邮件！");
                return;
            }
            if (this.txtbiaoti.Text.Trim() == "")
            {
                System.Windows.Forms.MessageBox.Show("请输入邮件标题！");
                return;
            }
            this.progressBar1.Value = 0;
            webBrowser1.Document.GetElementById("on").InvokeMember("Click");
            //string bb = aa.Replace(aa,@"<iframe class='ke-iframe' frameborder='' style='height: 141px; '>*</iframe>");
            string aa = webBrowser1.Document.GetElementById("text").InnerHtml;
            if (emailid == 0)
            {
                this.progressBar1.Maximum = mail.Count;
                for (int i = 0; i < mail.Count; i++)
                {
                    string[] go = mail[i].Split(',');

                    if (go[0] == null && go[1] == null)
                    {
                        break;
                    }
                    try
                    {
                        MailSender ms = new MailSender();
                        this.progressBar1.Value++;
                        ms.SendMail(go[1], this.txtFajianren.Text.Trim(), aa, this.txtbiaoti.Text.Trim(), this.txtFajianrenmima.Text.Trim());
                        ms.Send();
                        this.richTextBox1.Text += "用户：" + go[0] + ",邮箱：" + go[1] + ",状态：发送成功！ \n";
                    }
                    catch (Exception myException)
                    {
                        this.richTextBox1.Text += "用户：" + go[0] + ",邮箱：" + go[1] + ",状态：发送失败！原因：" + myException.Message + "\n";
                        continue;
                    }


                }
            }
            else
            {
                string[] print = this.txtshoujianren.Text.Split(',');

                this.progressBar1.Maximum = print.Length;
                for (int i = 0; i < print.Length; i++)
                {

                    if (print[0] == null)
                    {
                        break;
                    }
                    try
                    {
                        MailSender ms = new MailSender();
                        this.progressBar1.Value++;
                        ms.SendMail(print[0], this.txtFajianren.Text.Trim(), aa, this.txtbiaoti.Text.Trim(), this.txtFajianrenmima.Text.Trim());
                        ms.Send();
                        this.richTextBox1.Text += "邮箱：" + print[0] + ",状态：发送成功！ \n";
                    }
                    catch (Exception myException)
                    {
                        this.richTextBox1.Text += "邮箱：" + print[0] + ",状态：发送失败！原因：" + myException.Message + "\n";
                        continue;
                    }


                }
            }


            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }

        }
        int flag = 0;
        private void email_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            //Common.RawPrinterHelper.SendStringToPrinter("HP LaserJet Professional M1216nfh MFP", "asddddasdasdasdad3");
            



            if (bll.Update(this.txtFajianren.Text, this.txtFajianrenmima.Text))
            {
                System.Windows.Forms.MessageBox.Show("保存成功！");
            } 
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
               this.txtshoujianren.Text+=TransferData(fd.FileName, "sheet1");
            }
        }

        

        
        
    }
}
